package com.pt.entity;

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import com.pt.common.constant.TorrentConstant;
import lombok.Data;
import java.time.LocalDateTime;

@Data
@TableName("torrent")
public class Torrent {
    @TableId(type = IdType.AUTO)
    private Long id;
    
    private String title;
    
    private String description;
    
    private String category;
    
    private Long size;
    
    private String filePath;
    
    private String infoHash;
    
    private Long uploaderId;
    
    private Integer status; // 0: 待审核, 1: 已通过, 2: 已拒绝
    
    private String rejectReason;
    
    private Integer seeders;
    
    private Integer leechers;
    
    private Integer completes;
    
    private Integer downloads;
    
    private Integer views;
    
    private Boolean isPromotion; // 是否促销
    
    private Integer promotionType; // 促销类型: 0: 无, 1: 免费下载, 2: 2倍上传
    
    private Integer promotionStatus; // 促销状态
    
    private LocalDateTime promotionStartTime; // In the original there's only promotionEndTime

    private LocalDateTime promotionEndTime;
    
    private LocalDateTime createdTime;
    
    private LocalDateTime updatedTime;
    
    private Boolean isDeleted;
    
    private Integer categoryId; // 分类ID
    
    private Integer subCategoryId; // 子分类ID
    
    private Long fileSize; // 文件大小
    
    private Integer healthIndex; // 健康指数
    
    private Integer lastHealthIndex; // 上次健康指数
    
    private Integer finished; // 完成次数
    
    private LocalDateTime lastAction; // 最后活动时间
    
    @TableField(exist = false)
    private Double shareRatio;
    
    @TableField(exist = false)
    private Double popularity;
    
    @TableField(exist = false)
    private String uploaderName; // 上传者用户名，非数据库字段

    // 显式添加缺少的getter和setter方法
    public Integer getCategoryId() {
        return categoryId;
    }
    
    public void setCategoryId(Integer categoryId) {
        this.categoryId = categoryId;
    }
    
    public Integer getSubCategoryId() {
        return subCategoryId;
    }
    
    public void setSubCategoryId(Integer subCategoryId) {
        this.subCategoryId = subCategoryId;
    }
    
    public Long getFileSize() {
        return fileSize;
    }
    
    public void setFileSize(Long fileSize) {
        this.fileSize = fileSize;
    }
    
    public Integer getPromotionStatus() {
        return promotionStatus;
    }
    
    public void setPromotionStatus(Integer promotionStatus) {
        this.promotionStatus = promotionStatus;
    }
    
    public void setPromotionStatus(int promotionStatus) {
        this.promotionStatus = promotionStatus;
    }
    
    public LocalDateTime getPromotionStartTime() {
        return promotionStartTime;
    }
    
    public void setPromotionStartTime(LocalDateTime promotionStartTime) {
        this.promotionStartTime = promotionStartTime;
    }
    
    public LocalDateTime getPromotionEndTime() {
        return promotionEndTime;
    }
    
    public void setPromotionEndTime(LocalDateTime promotionEndTime) {
        this.promotionEndTime = promotionEndTime;
    }
    
    public LocalDateTime getLastAction() {
        return lastAction;
    }
    
    public void setLastAction(LocalDateTime lastAction) {
        this.lastAction = lastAction;
    }
    
    public Integer getHealthIndex() {
        return healthIndex;
    }
    
    public void setHealthIndex(int healthIndex) {
        this.healthIndex = healthIndex;
    }
    
    public Integer getLastHealthIndex() {
        return lastHealthIndex;
    }
    
    public void setLastHealthIndex(int lastHealthIndex) {
        this.lastHealthIndex = lastHealthIndex;
    }
    
    public Integer getFinished() {
        return finished;
    }
    
    public void setFinished(Integer finished) {
        this.finished = finished;
    }
    
    // 非数据库字段，用于显示
    public String getHealthStatus() {
        if (seeders == null || seeders == 0) {
            return "已死种";
        } else if (seeders < TorrentConstant.HEALTH_DANGER) {
            return "危险";
        } else if (seeders < TorrentConstant.HEALTH_NORMAL) {
            return "一般";
        } else {
            return "健康";
        }
    }
    
    public String getPromotionStatusText() {
        if (promotionType == null) {
            return "普通";
        }
        
        switch (promotionType) {
            case 0:
                return "普通";
            case 1:
                return "免费";
            case 2:
                return "2倍上传";
            default:
                return "未知";
        }
    }
    
    public String getSizeFormatted() {
        if (size == null || size == 0) {
            return "0 KB";
        }
        
        if (size < TorrentConstant.MB) {
            return String.format("%.2f KB", size / (double) TorrentConstant.KB);
        } else if (size < TorrentConstant.GB) {
            return String.format("%.2f MB", size / (double) TorrentConstant.MB);
        } else if (size < TorrentConstant.TB) {
            return String.format("%.2f GB", size / (double) TorrentConstant.GB);
        } else {
            return String.format("%.2f TB", size / (double) TorrentConstant.TB);
        }
    }
    
    public Double getShareRatio() {
        // 检查size是否为null，避免空指针异常
        if (size == null || size == 0) {
            return 0.0;
        }
        
        // 分享率计算：上传量/下载量的简化版本
        // 对于种子来说，可以用做种人数和下载人数的比例作为健康度指标
        if (leechers == null || leechers == 0) {
            return seeders != null && seeders > 0 ? Double.MAX_VALUE : 0.0;
        }
        
        if (seeders == null) {
            return 0.0;
        }
        
        return seeders.doubleValue() / leechers.doubleValue();
    }
    
    /**
     * 计算热度指数
     * 综合考虑下载数、评论数、完成数和最近活跃度
     */
    public Double getPopularity() {
        // 添加null检查
        int downloadsValue = downloads != null ? downloads : 0;
        int completesValue = completes != null ? completes : 0;
        int seedersValue = seeders != null ? seeders : 0;
        int leechersValue = leechers != null ? leechers : 0;
        
        // 计算基础热度
        double base = downloadsValue * 2 + completesValue * 3;
        
        // 考虑做种和下载者数量
        double activeScore = seedersValue * 10 + leechersValue * 5;
        
        // 时间衰减因子，最近更新的种子热度更高
        if (updatedTime == null) {
            return base + activeScore;  // 如果没有更新时间，就不应用时间衰减
        }
        
        long daysSinceLastAction = java.time.Duration.between(updatedTime, LocalDateTime.now()).toDays();
        double timeFactor = Math.max(0.1, 1.0 - (daysSinceLastAction / 30.0));
        
        return (base + activeScore) * timeFactor;
    }
} 